$(function () {
  var categoryid = getSearch().id
  var name;
  $.ajax({
    url: 'http://127.0.0.1:9090/api/getcategorybyid',
    dataType: 'json',
    data: {
      categoryid: categoryid
    },
    success: function (info) {
      console.log(info)
      $('.mmb_secondNav .title').html(template('tmp', info))
    }
  })

  var size;
  $.ajax({
    url: 'http://127.0.0.1:9090/api/getproductlist',
    dataType: 'json',
    data: {
      categoryid: categoryid,
      pageid: 1,
    },
    success: function (info) {
      console.log(info)
      $('.mmb_secondNav .content').html(template('tmp2', info))
      size = Math.ceil(info.totalCount / info.pagesize);
      console.log(size)
      var html = ''
      for (var i = 0; i < size; i++) {
        html += '<option value="' + (i + 1) + '">' + (i + 1) + '/' + size + '</option>'
        console.log(html)
      }
      $('#page').html(html)
      
    }
  })

  function render(page) {
    $.ajax({
      url: 'http://127.0.0.1:9090/api/getproductlist',
      dataType: 'json',
      data: {
        categoryid: categoryid,
        pageid: page || 1,
      },
      success: function (info) {
        $('.mmb_secondNav .content').html(template('tmp2', info))
      }
    })
  }


  $('#page').on('change', function () {
    var page = $('#page option:selected').val()
    render(page)
  })

  $('.paging').on('click','.prev',function() {
    var page = $('#page').val()
    if(page > 0) {
      page--
      $('#page option[value=' + page + ']').prop('selected',true)
      render(page)
    }
    
  })
  $('.paging').on('click','.next',function() {
    var page = $('#page').val()
    if( page < size ) {
      page++
      $('#page option[value=' + page + ']').prop('selected',true)
      render(page)
    } 
    
  })
})